package com.beici.waterghost.utils;

import com.mysql.cj.xdevapi.Client;
import com.qcloud.cos.COSClient;
import com.qcloud.cos.ClientConfig;
import com.qcloud.cos.auth.BasicCOSCredentials;
import com.qcloud.cos.auth.COSCredentials;
import com.qcloud.cos.http.HttpProtocol;
import com.qcloud.cos.model.ciModel.auditing.*;
import com.qcloud.cos.region.Region;
import com.tencentcloudapi.tsf.v20180326.models.CosCredentials;

import java.nio.charset.StandardCharsets;
import java.util.Base64;
import java.util.List;

/**
 * 检测文本工具
 */
public class contextUtils {

    // 检测文本是否不违法
    public static boolean  veifityContext(String context) {
        String secretId = "AKIDf9MIxKQFrFLAi00jBNIwJRJuDF0UgdPv";
        String secretKey = "IK5anjOmsgEUTAajbBUWHkwub07xWnnX";

        COSCredentials cosCredentials = new BasicCOSCredentials(secretId,secretKey);
        Region region = new Region("ap-guangzhou");
        ClientConfig clientConfig = new ClientConfig(region);
        clientConfig.setHttpProtocol(HttpProtocol.https);
        //1.创建任务请求对象
        TextAuditingRequest request = new TextAuditingRequest();
        //2.添加请求参数 参数详情请见api接口文档
        request.setBucketName("waterghosts-1308467424");
        //2.1.1设置对象地址
        //request.getInput().setObject("1.txt");
        //2.1.2或直接设置请求内容,文本内容的Base64编码
        Base64.Encoder encoder = Base64.getEncoder();
        String s = encoder.encodeToString(context.getBytes(StandardCharsets.UTF_8));
        request.getInput().setContent(s);
        //2.2设置审核类型参数
        request.getConf().setDetectType("all");
        //2.3设置审核模板（可选）
        //request.getConf().setBizType("aa3e9d84a6a079556b0109a935c*****");
        //3.调用接口,获取任务响应对象
        COSClient cosClient=new COSClient(cosCredentials,clientConfig);
        TextAuditingResponse auditingTextJobs = cosClient.createAuditingTextJobs(request);
        // 获取查询的jobId
        AuditingJobsDetail jobsDetail = auditingTextJobs.getJobsDetail();
        if (!jobsDetail.getResult().equals("0"))return false;
        return true;
    }

    public static boolean verfityImage(String url){
        String secretId = "AKIDf9MIxKQFrFLAi00jBNIwJRJuDF0UgdPv";
        String secretKey = "IK5anjOmsgEUTAajbBUWHkwub07xWnnX";
        COSCredentials cosCredentials = new BasicCOSCredentials(secretId,secretKey);
        Region region = new Region("ap-guangzhou");
        ClientConfig clientConfig = new ClientConfig(region);
        clientConfig.setHttpProtocol(HttpProtocol.https);
        ImageAuditingRequest imageAuditingRequest = new ImageAuditingRequest();
        imageAuditingRequest.setBucketName("waterghosts-1308467424");
        imageAuditingRequest.setDetectUrl(url);
        imageAuditingRequest.setDetectType("porn");
        COSClient cosClient=new COSClient(cosCredentials,clientConfig);
        ImageAuditingResponse response = cosClient.imageAuditing(imageAuditingRequest);
        String hitFlag = response.getPornInfo().getHitFlag();
        return hitFlag.equals("0")?true:false;
    }

    public static void main(String[] args) {
        verfityImage("https://waterghost.oss-cn-shenzhen.aliyuncs.com/1/2022-02-10/R-C%20(2).jpg");

    }
}
